package com.wzhq.pacsai;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.Config;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.wzhq.model.MainImages;
import org.slf4j.LoggerFactory;

/**
 * API引导式配置
 */
public class MainConfig {

    static org.slf4j.Logger LOG = LoggerFactory.getLogger(JFinalConfig.class);

    public MainConfig() {
    }


    /**
     * 配置路由
     */
    public void configRoute(Routes me) {
    }

    /*
    public static C3p0Plugin createC3p0Plugin() {
        return new C3p0Plugin(PropKit.get("OracleMainUrl"), PropKit.get("OracleMainUser"), PropKit.get("OracleMainPassword").trim());
    }
     */
    /**
     * 配置插件
     */
    public static void configPlugin() {
        PropKit.use("jdbc.properites");
        //initMssql();
        initMysql();
        initMysqlRis();
        initOraclel();
    }

    public static void initMysql() {
        //mysql 数据源
        ActiveRecordPlugin arpMysql = null;
        // WallFilter wallFilter = new WallFilter();
        String driver = PropKit.get("mysql.driver");
        String url = PropKit.get("mysql.url");
        String username = PropKit.get("mysql.username");
        String password = PropKit.get("mysql.password");
        DruidPlugin dpMysql = new DruidPlugin(url, username, password, driver);
        //dp.addFilter(new StatFilter());
        //dp.addFilter(wallFilter);
        //连接池最大使用连接数量
        dpMysql.setMaxActive(22);
        //连接池最小空闲
        dpMysql.setMinIdle(2);
        //初始化大小
        dpMysql.setInitialSize(2);

        arpMysql = new ActiveRecordPlugin("mysql", dpMysql);//设置数据库方言
        arpMysql.setDialect(new MysqlDialect());
        arpMysql.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写
        com.wzhq.model._MappingKit.mapping(arpMysql);

        //Config cfgMysql = new Config("mysql", dpMysql.getDataSource());
        // DbKit.addConfig(cfgMysql);
        //me.add(new EhCachePlugin());
        dpMysql.start();
        arpMysql.start();

        LOG.info("MysqlMain数据库初始化成功.............................");
    }

    public static void initMysqlRis() {
        //mysql 数据源
        ActiveRecordPlugin arpMysqlRIS = null;
        // WallFilter wallFilter = new WallFilter();
        String driver = PropKit.get("mysqlRis.driver");
        String url = PropKit.get("mysqlRis.url");
        String username = PropKit.get("mysqlRis.username");
        String password = PropKit.get("mysqlRis.password");
        DruidPlugin dpMysqlRIS = new DruidPlugin(url, username, password, driver);
        //dp.addFilter(new StatFilter());
        //dp.addFilter(wallFilter);
        //连接池最大使用连接数量
        dpMysqlRIS.setMaxActive(22);
        //连接池最小空闲
        dpMysqlRIS.setMinIdle(2);
        //初始化大小
        dpMysqlRIS.setInitialSize(2);

        arpMysqlRIS = new ActiveRecordPlugin("mysqlRis", dpMysqlRIS);
        //设置数据库方言
        arpMysqlRIS.setDialect(new MysqlDialect());
        arpMysqlRIS.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写
        //com.wzhq.model._MappingKit.mapping(arpMysqlRIS);

        //Config cfgMysql = new Config("mysql", dpMysqlRIS.getDataSource());
        // DbKit.addConfig(cfgMysql);
        //me.add(new EhCachePlugin());
        //arpMysqlRIS.setShowSql(true);
        dpMysqlRIS.start();
        arpMysqlRIS.start();

        LOG.info("MysqlRisMain数据库初始化成功.............................");
    }

    public static void main(String[] args) {
        configPlugin();
        new MainImages().set("StudyUID", "James").set("StudyName", 25).save();
        //new MainImages().set("title", "title").set("content", "cxt text").save();
        MainImages.dao.findById(123);
    }

    public static void initMssql() {
        //Mssql 数据源
        ActiveRecordPlugin arpMssql = null;
        // WallFilter wallFilter = new WallFilter();
        String driver = PropKit.get("sqlserver.driver");
        String url = PropKit.get("sqlserver.url");
        String username = PropKit.get("sqlserver.username");
        String password = PropKit.get("sqlserver.password");
        DruidPlugin dpMssql = new DruidPlugin(url, username, password, driver);
        //dp.addFilter(new StatFilter());
        //dp.addFilter(wallFilter);
        //连接池最大使用连接数量
        dpMssql.setMaxActive(22);
        //连接池最小空闲
        dpMssql.setMinIdle(2);
        //初始化大小
        dpMssql.setInitialSize(2);

        //Config cfgMssql = new Config("Mssql", dpMssql.getDataSource());
        // DbKit.addConfig(cfgMssql);
        arpMssql = new ActiveRecordPlugin("Mssql", dpMssql);//设置数据库方言
        arpMssql.setDialect(new SqlServerDialect());
        arpMssql.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写

        //arp.addMapping("users", "id", Users.class);
        //com.gsfy.herp.model.mssql._MappingKit.mapping(arpMssql);
        dpMssql.start();
        arpMssql.start();
        LOG.info("sqlserverMain数据库初始化成功.............................");
    }

    public static void initOraclel() {

        //ORACLE 数据源
        ActiveRecordPlugin arpOra = null;
        // WallFilter wallFilter = new WallFilter();
        String driver = PropKit.get("oracle.driver");
        String url = PropKit.get("oracle.url");
        String username = PropKit.get("oracle.username");
        String password = PropKit.get("oracle.password");
        DruidPlugin dpOra = new DruidPlugin(url, username, password, driver);
        //dp.addFilter(new StatFilter());
        //dp.addFilter(wallFilter);
        //连接池最大使用连接数量
        dpOra.setMaxActive(22);
        //连接池最小空闲
        dpOra.setMinIdle(2);
        //初始化大小
        dpOra.setInitialSize(2);

        // Config cfgOra = new Config("oracle", dpOra.getDataSource());
        //  DbKit.addConfig(cfgOra);
        arpOra = new ActiveRecordPlugin("oracle", dpOra);//设置数据库方言
        arpOra.setDialect(new OracleDialect());
        arpOra.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写
        //me.add(new EhCachePlugin());
        //arp.addMapping("users", "id", Users.class);
        //com.gsfy.MedImgDCD.model.oracle._MappingKit.mapping(arp);
        dpOra.start();
        arpOra.start();
        LOG.info("OracleMain数据库初始化成功.............................");
    }
    public static DruidPlugin createDruidPlugin() {
        PropKit.use("jdbc.properites");
        String driver = PropKit.get("mysql.driver");
        String url = PropKit.get("mysql.url");
        String username = PropKit.get("mysql.username");
        String password = PropKit.get("mysql.password");
        DruidPlugin dpMysql = new DruidPlugin(url, username, password, driver);
        return dpMysql;
    }
    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {

    }

    /**
     * 配置处理器
     */
    public void configHandler(Handlers me) {

    }

}
